package com.songtech.uitest.module.common;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.ModulesMapper;
import com.songtech.uitest.util.Space;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class KakongHandler {

    @Autowired
    private ModulesMapper mapper;

    @Autowired
    private Indent indent;

    private Logger logger = LoggerFactory.getLogger(KakongHandler.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     *
     * @param driver
     * @return
     */
    public boolean checkAndGo(WebDriver driver){

        AutoWait.setWebDriverWait(driver);

        boolean flag = false; //默认不弹
        boolean siteTypeflag = false; //默认不弹

        int t = indent.getStepPosition();
        String step = indent.getFlow().split(",")[t]; //step:当前流程步骤。类似"00100"

        try{
            String siteType = indent.getCurrentBuildType();
            if(siteType.equals("新址新建站") || siteType.equals("新址新建共享站") || siteType.equals("新址新建独享站")){
                siteTypeflag = true;
            }

            boolean approval = step.substring(3, 4).equals("0") ? true : false;
            String name = mapper.getClassMethod(step.substring(0, 3));
            //如果是submit且是同意就弹
            if(name.substring(name.length()-5).equals("ubmit")){
                if(approval){
                    flag = true;
                }
            }
            //如果是audit, 同时，上一次submit是同意，并且同意且是最后一级审批就弹
            if(name.substring(name.length()-5).equals("Audit")){
                //自己是同意
                if(approval){
                    //上一次submit是同意，且是最后一级审批

                    int f1 = 0; //代表同意与否
                    String[] steps = indent.getFlow().split(",");
                    int p = indent.getStepPosition();

                    String preName = "";
                    String nameTail = "";
                    while(p >= 0){ //查到上一次submit同意与否
                        p--;
                        preName = mapper.getClassMethod(steps[p].substring(0, 3));
                        nameTail = preName.substring(preName.length()-5);
                        if(nameTail.equals("ubmit")){
                            f1 = steps[p].substring(3, 4).equals("0") ? 0 : 1;
                            break;
                        }
                    }

                    //判断是不是最后一级审批
                    int f2 = 0; //默认是
                    int po = indent.getStepPosition();
                    String nextName = "";
                    String nameTal = "";
                    while(po + 1 < steps.length) { //如果不是最后一个step
                        po++;
                        nextName = mapper.getClassMethod(steps[po].substring(0, 3));
                        nameTal = nextName.substring(nextName.length() - 9);
                        if(nameTal.equals(name.substring(name.length()-9))){ //说明后边还有审批步骤
                            f2 = 1;
                            break;
                        }
                        else{
                            break;
                        }
                    }

                    if(f1 == 0 && f2 == 0){
                        flag = true;
                    }

                }
            }

//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

            if(flag == true && siteTypeflag == true) { //若该弹，则按弹的步骤走，出错则说明有问题
//                driver.findElement(By.cssSelector("div.modal.fade.delModel.in"));
                AutoWait.findByCss("div.modal.fade.delModel.in");

//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                driver.findElement(By.xpath("//button[text()='继续']")).click();
                AutoWait.findByXpath("//button[text()='继续']").click();
                logger.info(space.getSpace() + "检测到卡控正常弹出");
                return true;
            }
            else{ //若不该弹，就检查是否弹出，若弹出了则说明有问题
                driver.findElement(By.cssSelector("div.modal.fade.delModel.in"));

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                driver.findElement(By.xpath("//button[text()='继续']")).click();
                logger.error(space.getSpace() + "卡控弹出有误，本不该在此时弹出");
                return false;
            }

        }
        catch (NoSuchElementException e){
            if(flag == true && siteTypeflag == true){
                logger.error(space.getSpace() + "卡控没能正确弹出，此处应该弹出卡控");
                e.printStackTrace();
                return false;
            }
            else{
                logger.info(space.getSpace() + "卡控正确：此处卡控按逻辑没有出现");
                return true;
            }
        }
        catch (Exception e){
            if(flag == true && siteTypeflag == true){
                logger.error(space.getSpace() + "卡控判断为该弹出，但卡控出现了其它异常。");
                logger.error(space.getSpace() + "异常信息为：" + e.getMessage());
                e.printStackTrace();
                return false;
            }
            else{
                logger.info(space.getSpace() + "卡控判断为不该弹出，但卡控出现了其它异常。");
                logger.error(space.getSpace() + "异常信息为：" + e.getMessage());
                e.printStackTrace();
                return true;
            }
        }


    }
}
